<template>
  <div class="backend-test-page">
    <div class="page-header">
      <h1>Node.js 后端API测试</h1>
      <p>测试独立的Node.js后端服务接口</p>
    </div>
    
    <!-- 服务器状态 -->
    <div class="server-status">
      <div class="status-card">
        <h3>服务器状态</h3>
        <div class="status-info">
          <span class="status-label">后端地址:</span>
          <span class="status-value">http://localhost:3002</span>
        </div>
        <div class="status-info">
          <span class="status-label">连接状态:</span>
          <span :class="['status-value', serverConnected ? 'connected' : 'disconnected']">
            {{ serverConnected ? '已连接' : '未连接' }}
          </span>
        </div>
        <button @click="checkServerStatus" :disabled="checking" class="check-btn">
          {{ checking ? '检查中...' : '检查连接' }}
        </button>
      </div>
    </div>
    
    <div class="api-container">
      <!-- 天气API测试 -->
      <div class="api-section">
        <h2><i class="fa fa-cloud"></i> 天气API</h2>
        <div class="api-tabs">
          <button 
            @click="activeWeatherTab = 'basic'" 
            :class="{ active: activeWeatherTab === 'basic' }"
            class="tab-btn"
          >
            基础天气
          </button>
          <button 
            @click="activeWeatherTab = 'forecast'" 
            :class="{ active: activeWeatherTab === 'forecast' }"
            class="tab-btn"
          >
            天气预报
          </button>
          <button 
            @click="activeWeatherTab = 'current'" 
            :class="{ active: activeWeatherTab === 'current' }"
            class="tab-btn"
          >
            实时天气
          </button>
        </div>
        
        <div v-if="activeWeatherTab === 'basic'" class="api-form">
          <div class="input-group">
            <label>城市名称:</label>
            <input v-model="weatherForm.city" placeholder="如：广州" />
          </div>
          <div class="input-group">
            <label>坐标位置:</label>
            <input v-model="weatherForm.location" placeholder="如：113.289405,23.013125" />
          </div>
          <button @click="testWeatherApi" :disabled="loading" class="test-btn">
            <i class="fa fa-search"></i>
            {{ loading ? '请求中...' : '获取天气' }}
          </button>
        </div>
        
        <div v-if="activeWeatherTab === 'forecast'" class="api-form">
          <div class="input-group">
            <label>城市名称:</label>
            <input v-model="weatherForecastForm.city" placeholder="如：广州" />
          </div>
          <div class="input-group">
            <label>预报天数:</label>
            <input v-model="weatherForecastForm.days" type="number" placeholder="7" />
          </div>
          <button @click="testWeatherForecastApi" :disabled="loading" class="test-btn">
            <i class="fa fa-calendar"></i>
            {{ loading ? '请求中...' : '获取预报' }}
          </button>
        </div>
        
        <div v-if="activeWeatherTab === 'current'" class="api-form">
          <div class="input-group">
            <label>坐标位置:</label>
            <input v-model="currentWeatherForm.location" placeholder="如：113.289405,23.013125" />
          </div>
          <button @click="testCurrentWeatherApi" :disabled="loading" class="test-btn">
            <i class="fa fa-thermometer"></i>
            {{ loading ? '请求中...' : '获取实时天气' }}
          </button>
        </div>
        
        <div v-if="weatherResult" class="api-result">
          <h3>天气信息</h3>
          <pre>{{ JSON.stringify(weatherResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 地理编码API测试 -->
      <div class="api-section">
        <h2><i class="fa fa-map-marker"></i> 地理编码API</h2>
        <div class="api-tabs">
          <button 
            @click="activeGeocodeTab = 'basic'" 
            :class="{ active: activeGeocodeTab === 'basic' }"
            class="tab-btn"
          >
            基础编码
          </button>
          <button 
            @click="activeGeocodeTab = 'regeo'" 
            :class="{ active: activeGeocodeTab === 'regeo' }"
            class="tab-btn"
          >
            逆地理编码
          </button>
          <button 
            @click="activeGeocodeTab = 'geo'" 
            :class="{ active: activeGeocodeTab === 'geo' }"
            class="tab-btn"
          >
            地理编码
          </button>
        </div>
        
        <div v-if="activeGeocodeTab === 'basic'" class="api-form">
          <div class="input-group">
            <label>编码类型:</label>
            <select v-model="geocodeForm.type">
              <option value="geo">地址转坐标</option>
              <option value="regeo">坐标转地址</option>
            </select>
          </div>
          <div v-if="geocodeForm.type === 'geo'" class="input-group">
            <label>地址:</label>
            <input v-model="geocodeForm.address" placeholder="如：广州市天河区" />
          </div>
          <div v-if="geocodeForm.type === 'regeo'" class="input-group">
            <label>坐标:</label>
            <input v-model="geocodeForm.location" placeholder="如：113.289405,23.013125" />
          </div>
          <button @click="testGeocodeApi" :disabled="loading" class="test-btn">
            <i class="fa fa-map"></i>
            {{ loading ? '请求中...' : '地理编码' }}
          </button>
        </div>
        
        <div v-if="activeGeocodeTab === 'regeo'" class="api-form">
          <div class="input-group">
            <label>坐标:</label>
            <input v-model="regeocodeForm.location" placeholder="如：113.289405,23.013125" />
          </div>
          <button @click="testRegeocodeApi" :disabled="loading" class="test-btn">
            <i class="fa fa-location-arrow"></i>
            {{ loading ? '请求中...' : '逆地理编码' }}
          </button>
        </div>
        
        <div v-if="activeGeocodeTab === 'geo'" class="api-form">
          <div class="input-group">
            <label>地址:</label>
            <input v-model="geoForm.address" placeholder="如：广州市天河区" />
          </div>
          <button @click="testGeoApi" :disabled="loading" class="test-btn">
            <i class="fa fa-map-pin"></i>
            {{ loading ? '请求中...' : '地理编码' }}
          </button>
        </div>
        
        <div v-if="geocodeResult" class="api-result">
          <h3>地理编码结果</h3>
          <pre>{{ JSON.stringify(geocodeResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 错误信息显示 -->
      <div v-if="error" class="error-message">
        <i class="fa fa-exclamation-triangle"></i>
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';

// 使用后端API composable
const { 
  getWeather, getWeatherForecast, getCurrentWeather,
  getGeocode, getRegeocode, getGeo,
  getHealth, getServerInfo
} = useBackendApi();

// 响应式数据
const loading = ref(false);
const checking = ref(false);
const error = ref('');
const serverConnected = ref(false);

// 活跃标签页
const activeWeatherTab = ref('basic');
const activeGeocodeTab = ref('basic');

// 表单数据
const weatherForm = ref({
  city: '',
  location: ''
});

const weatherForecastForm = ref({
  city: '',
  days: 7
});

const currentWeatherForm = ref({
  location: ''
});

const geocodeForm = ref({
  type: 'regeo' as 'geo' | 'regeo',
  address: '',
  location: ''
});

const regeocodeForm = ref({
  location: ''
});

const geoForm = ref({
  address: ''
});

// 结果数据
const weatherResult = ref<any>(null);
const geocodeResult = ref<any>(null);

// 检查服务器状态
const checkServerStatus = async () => {
  checking.value = true;
  error.value = '';
  
  try {
    const health = await getHealth();
    const info = await getServerInfo();
    
    if (health.success && info.success) {
      serverConnected.value = true;
      console.log('服务器信息:', info);
    } else {
      serverConnected.value = false;
    }
  } catch (err) {
    console.error('服务器连接失败:', err);
    serverConnected.value = false;
    error.value = '无法连接到后端服务器，请确保服务器已启动';
  } finally {
    checking.value = false;
  }
};

// 天气API测试
const testWeatherApi = async () => {
  if (!weatherForm.value.city && !weatherForm.value.location) {
    error.value = '请输入城市名称或坐标';
    return;
  }
  
  loading.value = true;
  error.value = '';
  
  try {
    const params: any = {};
    if (weatherForm.value.city) params.city = weatherForm.value.city;
    if (weatherForm.value.location) params.location = weatherForm.value.location;
    
    const response = await getWeather(params);
    weatherResult.value = response;
  } catch (err) {
    console.error('天气API测试失败:', err);
    error.value = '天气API调用失败';
  } finally {
    loading.value = false;
  }
};

const testWeatherForecastApi = async () => {
  loading.value = true;
  error.value = '';
  
  try {
    const params: any = {};
    if (weatherForecastForm.value.city) params.city = weatherForecastForm.value.city;
    if (weatherForecastForm.value.days) params.days = weatherForecastForm.value.days;
    
    const response = await getWeatherForecast(params);
    weatherResult.value = response;
  } catch (err) {
    console.error('天气预报API测试失败:', err);
    error.value = '天气预报API调用失败';
  } finally {
    loading.value = false;
  }
};

const testCurrentWeatherApi = async () => {
  loading.value = true;
  error.value = '';
  
  try {
    const params: any = {};
    if (currentWeatherForm.value.location) params.location = currentWeatherForm.value.location;
    
    const response = await getCurrentWeather(params);
    weatherResult.value = response;
  } catch (err) {
    console.error('实时天气API测试失败:', err);
    error.value = '实时天气API调用失败';
  } finally {
    loading.value = false;
  }
};

// 地理编码API测试
const testGeocodeApi = async () => {
  if (geocodeForm.value.type === 'geo' && !geocodeForm.value.address) {
    error.value = '请输入地址';
    return;
  }
  if (geocodeForm.value.type === 'regeo' && !geocodeForm.value.location) {
    error.value = '请输入坐标';
    return;
  }
  
  loading.value = true;
  error.value = '';
  
  try {
    const params: any = { type: geocodeForm.value.type };
    if (geocodeForm.value.address) params.address = geocodeForm.value.address;
    if (geocodeForm.value.location) params.location = geocodeForm.value.location;
    
    const response = await getGeocode(params);
    geocodeResult.value = response;
  } catch (err) {
    console.error('地理编码API测试失败:', err);
    error.value = '地理编码API调用失败';
  } finally {
    loading.value = false;
  }
};

const testRegeocodeApi = async () => {
  if (!regeocodeForm.value.location) {
    error.value = '请输入坐标';
    return;
  }
  
  loading.value = true;
  error.value = '';
  
  try {
    const response = await getRegeocode({ location: regeocodeForm.value.location });
    geocodeResult.value = response;
  } catch (err) {
    console.error('逆地理编码API测试失败:', err);
    error.value = '逆地理编码API调用失败';
  } finally {
    loading.value = false;
  }
};

const testGeoApi = async () => {
  if (!geoForm.value.address) {
    error.value = '请输入地址';
    return;
  }
  
  loading.value = true;
  error.value = '';
  
  try {
    const response = await getGeo({ address: geoForm.value.address });
    geocodeResult.value = response;
  } catch (err) {
    console.error('地理编码API测试失败:', err);
    error.value = '地理编码API调用失败';
  } finally {
    loading.value = false;
  }
};

// 页面加载时检查服务器状态
onMounted(() => {
  checkServerStatus();
});
</script>

<style scoped>
.backend-test-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.page-header {
  text-align: center;
  color: white;
  margin-bottom: 30px;
}

.page-header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  font-weight: 300;
}

.page-header p {
  font-size: 1.1rem;
  opacity: 0.9;
}

.server-status {
  max-width: 1200px;
  margin: 0 auto 30px;
}

.status-card {
  background: white;
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.status-card h3 {
  color: #333;
  margin-bottom: 15px;
  font-size: 1.3rem;
}

.status-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}

.status-label {
  font-weight: 600;
  color: #555;
}

.status-value {
  color: #333;
}

.status-value.connected {
  color: #28a745;
  font-weight: 600;
}

.status-value.disconnected {
  color: #dc3545;
  font-weight: 600;
}

.check-btn {
  margin-top: 15px;
  padding: 8px 16px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s;
}

.check-btn:hover:not(:disabled) {
  background: #5a6fd8;
}

.check-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.api-container {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  gap: 30px;
}

.api-section {
  background: white;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.api-section h2 {
  color: #333;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.5rem;
}

.api-section h2 i {
  color: #667eea;
}

.api-tabs {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
  border-bottom: 2px solid #f0f0f0;
  padding-bottom: 10px;
}

.tab-btn {
  padding: 8px 16px;
  background: #f8f9fa;
  color: #666;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
}

.tab-btn.active {
  background: #667eea;
  color: white;
}

.tab-btn:hover {
  background: #5a6fd8;
  color: white;
}

.api-form {
  display: grid;
  gap: 15px;
  margin-bottom: 20px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.input-group label {
  font-weight: 600;
  color: #555;
  font-size: 14px;
}

.input-group input,
.input-group select {
  padding: 12px 15px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s;
}

.input-group input:focus,
.input-group select:focus {
  border-color: #667eea;
}

.test-btn {
  padding: 12px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.test-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.test-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.api-result {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  border-left: 4px solid #667eea;
}

.api-result h3 {
  color: #333;
  margin-bottom: 15px;
  font-size: 1.2rem;
}

.api-result pre {
  background: #2d3748;
  color: #e2e8f0;
  padding: 15px;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 12px;
  line-height: 1.4;
}

.error-message {
  background: #fff5f5;
  color: #e53e3e;
  padding: 15px;
  border-radius: 8px;
  border: 1px solid #fed7d7;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  margin-top: 20px;
}

@media (max-width: 768px) {
  .page-header h1 {
    font-size: 2rem;
  }
  
  .api-container {
    gap: 20px;
  }
  
  .api-section {
    padding: 20px;
  }
  
  .api-tabs {
    flex-wrap: wrap;
  }
  
  .status-info {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
}
</style> 